package com.xthh._function._ini;

import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.xt.xthh.R;
import com.xthh._enum.HH_EnumDeviceColorType;
import com.xthh._function.HH_CharUtil;
import com.xthh._function.HH_FileUtil;
import com.xthh._function.HH_SystemUtil;
import com.xthh._struct.HH_T_BindDevice;
import com.xthh._struct.HH_T_DeviceColorType;
import com.xthh._struct.HH_T_TagImportParams;
import com.xthh.sql.HH_BindDeviceDao2;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class HH_INIBindDeviceInfo {

    private static final String TAG = "INIBindDeviceInfo";


    public static String GetBindDeviceFolder(Application api) {
        return HH_SystemUtil.GetAppRootFolder(api) + "ini" + File.separator;
    }

    public static void DelBindDeviceName(Application api, Context ctx, HH_T_BindDevice tbd) {
        if (tbd == null)
            return;
        HH_BindDeviceDao2 dao = new HH_BindDeviceDao2(ctx, api);
        dao.delete(tbd.getMacRes());
    }

    public static void ChangeBindDeviceName(Application api, Context ctx, HH_T_BindDevice tbd) {
        if (tbd == null)
            return;
        HH_BindDeviceDao2 dao = new HH_BindDeviceDao2(ctx, api);
        dao.update_name(tbd.getMacRes(), tbd.getName());
    }

    // 获取所有绑定设备
    public static List<HH_T_BindDevice> GetBindDevices(Application api, Context ctx) {
        List<HH_T_BindDevice> list = new ArrayList<>();
        HH_BindDeviceDao2 dao = new HH_BindDeviceDao2(ctx, api);
        list = dao.queryAll();
        return list;
    }

    // 都是以MAC地址为文件名方式写入的
    public static void WriteBindDevice(Application api, Context ctx, HH_T_BindDevice tbd, boolean writeName) {
        HH_BindDeviceDao2 dao = new HH_BindDeviceDao2(ctx, api);
        HH_T_BindDevice t = dao.queryItem(tbd.getMacRes());
        if (t != null){
            // 不修改名称
            dao.delete(tbd.getMacRes());
            String oldName = t.getName();
            t = tbd;
            t.setName(oldName);

            dao.insert(t, writeName);
        } else {
            dao.insert(tbd, writeName);
        }
    }

    public static void WritePhoneCaseInfo(Application api, Context ctx, HH_T_TagImportParams tti) {
        String savePath = HH_SystemUtil.GetAppRootFolder(api) + "ini" + File.separator;

        String strNameMac = HH_CharUtil.GetMacFormStr(tti.getMac());
        if (strNameMac == null || strNameMac.length() != 12)
            return;

        String strMacPath = String.format("%s%s%s", savePath, strNameMac, File.separator);
        HH_FileUtil.CreateFolder(strMacPath);
        HH_FileUtil.WriteFile(ctx, strMacPath + "NAME.txt", String.format("%s", tti.getName()), false);
        HH_FileUtil.WriteFile(ctx, strMacPath + "DEVICECOLORTYPE.txt", String.format("%s", tti.getColor().getColorType() != null ? tti.getColor().getColorType().toString() : ""), false);
        HH_FileUtil.WriteFile(ctx, strMacPath + "DEVICEWIDTH.txt", String.format("%d", tti.getWidth()), false);
        HH_FileUtil.WriteFile(ctx, strMacPath + "DEVICEHEIGHT.txt", String.format("%d", tti.getHeight()), false);
        HH_FileUtil.WriteFile(ctx, strMacPath + "DEVICENUMBER.txt", String.format("%d", tti.getDeviceNumber()), false);
    }


    public static HH_T_TagImportParams GetPhoneCaseInfo(Application api, Context ctx) {
        String savePath = HH_SystemUtil.GetAppRootSaveFolder(api, "ini");

        List<String> macsPath = HH_FileUtil.SearchFiles(savePath, false);
        if (macsPath != null && macsPath.size() > 0){
            String strSrcPath = macsPath.get(0);
            if (strSrcPath != null && strSrcPath.length() > 0){
                String[] tb = strSrcPath.split(savePath);
                if (tb != null && tb.length == 2){
                    String strBindDeviceMac = HH_CharUtil.GetMacFormStr(tb[1]);
                    if (strBindDeviceMac != null && strBindDeviceMac.length() == 12){
                        String NAME = HH_FileUtil.ReadFile(ctx, String.format("%s%s%s", strSrcPath, File.separator, "NAME.txt"));
                        String DEVICECOLORTYPE = HH_FileUtil.ReadFile(ctx, String.format("%s%s%s", strSrcPath, File.separator, "DEVICECOLORTYPE.txt"));
                        String DEVICEWIDTH = HH_FileUtil.ReadFile(ctx, String.format("%s%s%s", strSrcPath, File.separator, "DEVICEWIDTH.txt"));
                        String DEVICEHEIGHT = HH_FileUtil.ReadFile(ctx, String.format("%s%s%s", strSrcPath, File.separator, "DEVICEHEIGHT.txt"));
                        String DEVICENUMBER = HH_FileUtil.ReadFile(ctx, String.format("%s%s%s", strSrcPath, File.separator, "DEVICENUMBER.txt"));

                        HH_T_TagImportParams ttip = new HH_T_TagImportParams();
                        ttip.setName(NAME);
                        ttip.setWidth(HH_CharUtil.Atoi(DEVICEWIDTH));
                        ttip.setHeight(HH_CharUtil.Atoi(DEVICEHEIGHT));
                        ttip.setMac(strBindDeviceMac);
                        ttip.setDeviceNumber(HH_CharUtil.Atoi(DEVICENUMBER));
                        if (HH_CharUtil.Strcmp(DEVICECOLORTYPE, "BLACK_WHITE_0", true))
                            ttip.setColor(new HH_T_DeviceColorType(HH_EnumDeviceColorType.BLACK_WHITE_0, ctx.getString(R.string.strID_218)));
                        else if (HH_CharUtil.Strcmp(DEVICECOLORTYPE, "BLACK_WHITE_YELLOW_1", true))
                            ttip.setColor(new HH_T_DeviceColorType(HH_EnumDeviceColorType.BLACK_WHITE_YELLOW_1, ctx.getString(R.string.strID_219)));
                        else if (HH_CharUtil.Strcmp(DEVICECOLORTYPE, "BLACK_WHITE_RED_2", true))
                            ttip.setColor(new HH_T_DeviceColorType(HH_EnumDeviceColorType.BLACK_WHITE_RED_2, ctx.getString(R.string.strID_220)));
                        else if (HH_CharUtil.Strcmp(DEVICECOLORTYPE, "BLACK_WHITE_RED_YELLOW_3", true))
                            ttip.setColor(new HH_T_DeviceColorType(HH_EnumDeviceColorType.BLACK_WHITE_RED_YELLOW_3, ctx.getString(R.string.strID_221)));
                        else if (HH_CharUtil.Strcmp(DEVICECOLORTYPE, "N_COLOR_4", true))
                            ttip.setColor(new HH_T_DeviceColorType(HH_EnumDeviceColorType.N_COLOR_4, ctx.getString(R.string.strID_222)));
                        else if (HH_CharUtil.Strcmp(DEVICECOLORTYPE, "E6_5", true))
                            ttip.setColor(new HH_T_DeviceColorType(HH_EnumDeviceColorType.E6_5, ctx.getString(R.string.strID_443)));
                        else
                            ttip.setColor(new HH_T_DeviceColorType(HH_EnumDeviceColorType.NONE, "NONE"));
                        return ttip;


                    }
                }
            }
        }
        return null;
    }
}
